package com.jiashihui.web.mybatis;

import com.jiashihui.web.model.Dept;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.io.IOException;

/**
 * @ClassName TestOneLevelCache
 * @Author Administrator
 * 测试一级缓存
 * @Version 1.0
 **/
public class TestOneLevelCache {

    /*
     * 一级缓存: 也就Session级的缓存(默认开启)
     */
    @Test
    public void testCache1() throws IOException{
        SqlSession session = MyBatisUtil.getSqlSession();
        String statement = "com.jiashihui.web.mapper.DeptMapper.getById";
        Dept dept = session.selectOne(statement, 1);
        System.out.println(dept);

        /*
         * 一级缓存默认就会被使用
         */
        dept = session.selectOne(statement, 1);
        System.out.println(dept);
        session.close();
        /*
         1. 必须是同一个Session,如果session对象已经close()过了就不可能用了
         */
        session = MyBatisUtil.getSqlSession();
        dept = session.selectOne(statement, 1);
        System.out.println(dept);

        /*
         2. 查询条件是一样的
         */
        dept = session.selectOne(statement, 2);
        System.out.println(dept);

        /*
         3. 没有执行过session.clearCache()清理缓存
         */
        //session.clearCache();
        dept = session.selectOne(statement, 2);
        System.out.println(dept);

        /*
         4. 没有执行过增删改的操作(这些操作都会清理缓存)
         */
//        session.update("com.jiashihui.web.mapper.DeptMapper.updateUser", new Dept(2, "user", ""));
//        dept = session.selectOne(statement, 2);
//        System.out.println(dept);

    }
}
